package adapletplugin.dialogs;


import objects.AdapletConstants;
import objects.AdapletConstants.ColumnType;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewColumnDialog extends TitleAreaDialog
{
	private String fColumnName;
	private ColumnType fColumnType;
	private int fColumnSize;
	
	private Text fColumnNameText;
	private Text fColumnSizeText;
	private Combo fColumnTypeCombo;

	public NewColumnDialog(Shell parentShell)
	{
		super(parentShell);
		
		fColumnName = "";
		fColumnType = ColumnType.STRING;
		fColumnSize = 0;
	}
	
	@Override
	public void create()
	{
		super.create();
		
		setTitle("New Column");
		setMessage("Create a new column:");
		
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
	}
	
	@Override
	protected Control createDialogArea(Composite parent)
	{
		parent.setLayout(new GridLayout());
		
		Composite body = new Composite(parent, SWT.NULL);
		body.setLayout(new GridLayout(2, false));
		body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Label label = new Label(body, SWT.NULL);
		label.setText("Column Name");
		
		fColumnNameText = new Text(body, SWT.BORDER);
		fColumnNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fColumnNameText.setText("");
		fColumnNameText.addModifyListener(createModifyListener());
		
		label = new Label(body, SWT.NULL);
		label.setText("Column Type:");
		
		fColumnTypeCombo = new Combo(body, SWT.READ_ONLY | SWT.BORDER | SWT.DROP_DOWN);
		fColumnTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		
		fColumnTypeCombo.add(AdapletConstants.convertTypeToString(ColumnType.INT), ColumnType.INT.ordinal());
		fColumnTypeCombo.add(AdapletConstants.convertTypeToString(ColumnType.REAL), ColumnType.REAL.ordinal());
		fColumnTypeCombo.add(AdapletConstants.convertTypeToString(ColumnType.STRING), ColumnType.STRING.ordinal());
		fColumnTypeCombo.add(AdapletConstants.convertTypeToString(ColumnType.DATE), ColumnType.DATE.ordinal());
		fColumnTypeCombo.add(AdapletConstants.convertTypeToString(ColumnType.LONG), ColumnType.LONG.ordinal());
		fColumnTypeCombo.add(AdapletConstants.convertTypeToString(ColumnType.RAW), ColumnType.RAW.ordinal());
		fColumnTypeCombo.add(AdapletConstants.convertTypeToString(ColumnType.LARGESTRING), ColumnType.LARGESTRING.ordinal());
		
		fColumnTypeCombo.addSelectionListener(createSelectionListener());
		
		fColumnTypeCombo.select(ColumnType.STRING.ordinal());
		
		label = new Label(body, SWT.NULL);
		label.setText("Size (for largestring columns):");
		
		fColumnSizeText = new Text(body, SWT.BORDER);
		fColumnSizeText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fColumnSizeText.setEnabled(false);
		fColumnSizeText.addModifyListener(createModifyListener());
		
		return parent;
	}

	private SelectionListener createSelectionListener()
	{
		return new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				if(fColumnTypeCombo.getSelectionIndex() == ColumnType.LARGESTRING.ordinal())
				{
					fColumnSizeText.setEnabled(true);
					fColumnSizeText.setText("");
				} 
				else
				{
					fColumnSizeText.setEnabled(false);
					fColumnSizeText.setText("");
				}
			}
		};
	}

	private ModifyListener createModifyListener()
	{
		return new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e)
			{
				if(fColumnNameText.getText().isEmpty())
				{
					setErrorMessage("Column Name cannot be empty");
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else if(fColumnTypeCombo.getSelectionIndex() == ColumnType.LARGESTRING.ordinal())
				{
					String curText = fColumnSizeText.getText();
					if(curText.matches("[0-9]+"))
					{
						setErrorMessage(null);
						getButton(IDialogConstants.OK_ID).setEnabled(true);
					}
					else
					{
						setErrorMessage("For largestring columns the size must be an integer");
						getButton(IDialogConstants.OK_ID).setEnabled(false);
					}
				}
				else
				{
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
				
			}
		};
	}
	
	@Override
	protected void okPressed()
	{

		fColumnName = fColumnNameText.getText();
		
		fColumnSize = fColumnSizeText.isEnabled() ? Integer.parseInt(fColumnSizeText.getText()) : 0;
		
		fColumnType = ColumnType.values()[fColumnTypeCombo.getSelectionIndex()];
		
		super.okPressed();
	}

	public int getColumnSize()
	{
		return fColumnSize;
	}

	public ColumnType getColumnType()
	{
		return fColumnType;
	}

	public String getColumnName()
	{
		return fColumnName;
	}

}
